import 'package:flutter/material.dart';
import 'package:flutter_bsbdj/bean/music_radio.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_bsbdj/pagers/music/music_channel_gg_detail.dart';
import 'package:flutter_bsbdj/pagers/music/music_channel_man_detail.dart';
import 'package:flutter_bsbdj/pagers/music/music_suspension_window.dart';
import 'package:flutter_bsbdj/utils/widget_views.dart';

class ChannelList extends StatefulWidget {
  List<RadioItem> _data;
  int cid;
  String typeName;

  ChannelList(this._data, this.cid, {this.typeName});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return ChannelWidget();
  }
}

class ChannelWidget extends State<ChannelList> {
  double _marginBottom = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _changMarginBottom();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.typeName),
      ),
      body: Container(
        margin: EdgeInsets.only(bottom: _marginBottom),
        child: GridView.builder(
          itemCount: widget._data.length,
          itemBuilder: (content, index) {
            RadioItem item = widget._data[index];
            return GestureDetector(
              child: Container(
                margin: EdgeInsets.all(5),
                color: Colors.white,
                padding: EdgeInsets.only(top: 5),
                child: Stack(
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.only(bottom: 20),
                      alignment: Alignment.center,
                      child: WidgetViews.getInstance().cachedImage(
                          widget.cid == 1 ? item.thumb : item.avatar),
                    ),
                    Container(
                      alignment: Alignment.bottomCenter,
                      child: Container(
                        height: 20,
                        child: Text(
                          item.name,
                          style: TextStyle(color: Colors.black54, fontSize: 14),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              onTap: () {
                if (widget.cid == 1) {
                  Navigator.of(context).push(MaterialPageRoute(
                      builder: (context) =>
                          ChannelGGDetail(item.name, item.ch_name)));
                } else {
                  Navigator.of(context).push(MaterialPageRoute(
                      builder: (context) =>
                          ChannelManDetail(item.name, item.name)));
                }
              },
            );
          },
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
          ),
        ),
      ),
    );
  }

  void _changMarginBottom() async {
    await Future.delayed(Duration(milliseconds: 400), () {
      setState(() {
        if (MusicWindowPlay.getInstance(context).musicIsPlaying()) {
          _marginBottom = 60;
          MusicWindowPlay.getInstance(context).isHeidden(false);
        } else {
          _marginBottom = 0;
        }
      });
    });
  }
}
